<template>
  <div>
    <div>
      <m-button
        color="#b4272d"
        styles="margin:0.3733rem 0.3733rem 0 0.3733rem;height:30px;"
        @click="createAddress">
        <span @click="createAddress">新建收货地址</span>

      </m-button>
    </div>
    <div class="address-item border-1px-bottom" v-for="item in address_list" >
<!--      <div class="item-select" @click="chooseAddress">-->
<!--        <i class="iconfont">&#xe65f;</i>-->
<!--      </div>-->
      <div class="item-main" @click="chooseAddress($event,item)">
        <div class="main-top">
          <span>{{item.delivery_user}}</span>
          <span>{{item.phone}}</span>
        </div>
        <div class="main-bottom">
<!--          <span>默认</span>-->
          <p>{{item.address}} {{item.detailed}}</p>
        </div>
      </div>
      <div class="item-update" @click="update($event,item)">
        <i class="iconfont">&#xe62c;</i>
      </div>
    </div>

  </div>

</template>

<script type="text/ecmascript-6">
export default {
  data(){
    return {
      address_list:[]
    }
  },
  created() {
    this.addressList();
  },
  methods: {
    update (e,item) {
      if ( e && e.preventDefault ) {
        e.preventDefault()//非IE浏览器
      } else { window.event.returnValue = false; } //IE浏览器
      this.$emit('update',item)
    },
    chooseAddress(event,row){
      console.log(this.$route.query.from)
      if(this.$route.query.from === 'order'){
        let url = '/checkout?address_id='+row.id;
        let allQuery = this.$route.query;
        for(let i in allQuery){
          if(i !== 'from'){
            url+=("&"+i+'='+allQuery[i]);
          }
        }
        this.$go(url,false)
      }
    },
    createAddress(){
      this.$emit('create')
    },
    addressList(){
      this.doRequest.get('address_list').then(ret=>{
          this.address_list = ret.data;
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .address-item{
    display: flex;
    height: 210px;
    padding:0 27px;
    background: white;
    .item-select{
      display: flex;
      align-items: center;
      flex-grow: 0;
      i{
        color:#d74945;
      }
    }
    .item-main{
      display: flex;
      flex-direction: column;
      justify-content: center;
      flex:1;
      padding:0 24px 0 40px;
      .main-top{
        display: flex;
        justify-content: space-between;
        span{
          font-weight: 600;
          font-size: 28px;
          color:#333;
        }
      }
      .main-bottom{
        display: flex;
        padding-top:27px;
        span{
          display: inline-block;
          height: 36px;
          box-sizing: border-box;
          padding:0 10px;
          margin-right:10px;
          line-height: 34px;
          border:1px solid #d74945;
          white-space: nowrap;
          color:#d74945;
        }
        p{
          color:#666;
        }
      }
    }
    .item-update{
      display: flex;
      align-items: center;
      flex-grow: 0;
      i{
        color:#666;
      }
    }
  }
</style>
